<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" />
	<title>支付页面</title>
	<link rel="stylesheet" type="text/css" href="css/public.css">
	<link rel="stylesheet" type="text/css" href="css/common.css">
	<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
	<script src="lib/urijs/URI.min.js"></script>
	<script src="js/jquery-3.1.1.min.js"></script>
	<script src="js/moment.min.js"></script>
	<script src="js/vconsole.min.js"></script>
	<script src="js/weixin-common.js" check_auth></script>
	<script src="js/app-common.js"></script>
</head>
<body>
	<div class="cont">
		<div class="fix">
			<div class="tips">
				启动微信支付...
			</div>
		</div>
	</div>
	<div class="fail">
		<h2 class="unfinished">支付失败</h2>
		<div class="info big">
			<p class="name"><span>产品名称</span><span class="proName"></span></p>
			<p class="money"><span>金<b>金额</b>额</span><span class="proPrice"></span></p>
			<p class="code"><span>订单号码</span><span class="proCode"></span></p>
			<p class="time"><span>交易时间</span><span class="proTime"></span></p>
		</div>
		<div class="btn">
			<button class="emit">重新支付</button>
		</div>
	</div>
</body>
<script>
	var params = getParams();
	var promoterId = params.promoter_id;
	var productId = params.product_id;

	if (!promoterId || !productId) {
		$(".fix .tips").text('请通过二维码购买');
	}

	$(".fix").show();

	$(function() {
		$(".emit").click(function() {
			pay();
		});

		pay();
	});

	function pay() {
		if (parent == window) {
			return;
		}
		parent.onBridgeReady(function() {
			WeixinJSBridge = parent.WeixinJSBridge;
			onBridgeReady();
		});
	}

	function onBridgeReady() {
		$.ajax({
			type: "POST",
			url: APP_CONFIG.acquireOrderUrl,
			data: {
				token: getUserToken(),
				promoter_id: promoterId,
				product_id: productId
			},
			success: function(res) {
				if (checkServiceResult(res, function() {
					$(".fix .tips").text(res.result.message);
				})) {
					var info = res.data;

					localStorage.setItem("orderid", info["order_id"]);

					WeixinJSBridge.invoke(
						'getBrandWCPayRequest', {
							"appId": info.appid, // 公众号名称，由商户传入
							"timeStamp": info.time_stamp, // 时间戳，自1970年以来的秒数
							"nonceStr": info.nonce_str, // 随机串     
							"package": info.prepay_id,
							"signType": info.sign_type, // 微信签名方式
							"paySign": info.pay_sign // 微信签名
						},
						function(res) {
							//console.log(res);
							if (res.err_msg == "get_brand_wcpay_request:ok") {
								parent.location.replace(URI().directory() + '/success.html');
							} else {
								$(".fail").show();
								var createAt = moment(info.create_at).format("YYYY-MM-DD  hh:mm:ss");
								$(".proName").text(info.product_name);
								$(".proPrice").text(((info.order_fee) * 0.01).toFixed(2) + "元");
								$(".proCode").text(info.out_trade_no);
								$(".proTime").text(createAt);
							}
						}
					);
				}
			}
		});
	}
</script>
</html>